package com.qigao.mall.web.controller.admin.merchant;

import com.github.pagehelper.PageInfo;
import com.qigao.mall.api.ProfitDetailService;
import com.qigao.mall.api.SettleTotalService;
import com.qigao.mall.commons.dto.*;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.utils.excel.ExcelFormat;
import com.qigao.mall.commons.utils.excel.ExcelHeaderInfo;
import com.qigao.mall.commons.utils.excel.ExcelUtils;
import com.qigao.mall.web.controller.AbstractExcelController;
import com.qigao.mall.web.vo.admin.merchant.ProfitCondVo;
import com.qigao.mall.web.vo.admin.merchant.SettleCondVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/profit/")
@Api(tags = "Admin商户操作分润相关API" ,value = "Admin商户操作分润相关API")
public class AdminProfitController extends AbstractExcelController {

    private static Logger logger= LoggerFactory.getLogger(AdminProfitController.class);
    @Resource
    private ProfitDetailService profitDetailService;

    @PostMapping("/listPage")
    @ApiOperation(value = "按照分润状态查询列表", notes = "按照分润状态查询列表")
    public Result<PageInfo<ProfitDetailResultDto>> listPage(@RequestBody ProfitCondVo profitCondVo, HttpServletRequest request){
        try{
            //参数校验
            ProfitCondDto profitCondDto=new ProfitCondDto();
            BeanUtils.copyProperties(profitCondVo,profitCondDto);
            if(StringUtils.isNotEmpty(profitCondVo.getDateBegin())){
                profitCondDto.setDateBegin(profitCondVo.getDateBegin()+" 00:00:00");
            }
            if(StringUtils.isNotEmpty(profitCondVo.getDateEnd())){
                profitCondDto.setDateEnd(profitCondVo.getDateEnd()+" 23:59:59");
            }
            return  profitDetailService.listPage(profitCondDto);
        }catch (Exception e){
            logger.error("[按照分润状态查询列表]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    @GetMapping("/export")
    @ApiOperation(value = "导出Excel", notes = "导出Excel")
    public void exportGet(ProfitCondVo profitCondVo, HttpServletResponse response) {
        ProfitCondDto profitCondDto=new ProfitCondDto();
        BeanUtils.copyProperties(profitCondVo,profitCondDto);
        if(StringUtils.isNotEmpty(profitCondVo.getDateBegin())){
            profitCondDto.setDateBegin(profitCondVo.getDateBegin()+" 00:00:00");
        }
        if(StringUtils.isNotEmpty(profitCondVo.getDateEnd())){
            profitCondDto.setDateEnd(profitCondVo.getDateEnd()+" 23:59:59");
        }
        // 待导出数据
        List<ProfitDetailResultDto> orderResultDtos = profitDetailService.findOrderResultByCond(profitCondDto);
        ExcelUtils excelUtils = new ExcelUtils(orderResultDtos, getHeaderInfo(), getFormatInfo());
        sendHttpResponse(response, "profitReport", excelUtils.getWorkbook());
    }

    @PostMapping("/export")
    @ApiOperation(value = "导出Excel", notes = "导出Excel")
    public void export(@RequestBody ProfitCondVo profitCondVo, HttpServletResponse response) {
        ProfitCondDto profitCondDto=new ProfitCondDto();
        BeanUtils.copyProperties(profitCondVo,profitCondDto);
        if(StringUtils.isNotEmpty(profitCondVo.getDateBegin())){
            profitCondDto.setDateBegin(profitCondVo.getDateBegin()+" 00:00:00");
        }
        if(StringUtils.isNotEmpty(profitCondVo.getDateEnd())){
            profitCondDto.setDateEnd(profitCondVo.getDateEnd()+" 23:59:59");
        }
        // 待导出数据
        List<ProfitDetailResultDto> orderResultDtos = profitDetailService.findOrderResultByCond(profitCondDto);
        ExcelUtils excelUtils = new ExcelUtils(orderResultDtos, getHeaderInfo(), getFormatInfo());
        sendHttpResponse(response, "profitReport", excelUtils.getWorkbook());
    }

    // 获取表头信息
    private List<ExcelHeaderInfo> getHeaderInfo() {
        return Arrays.asList(
                new ExcelHeaderInfo(0, 0, 0, 0, "结算日期"),
                new ExcelHeaderInfo(0, 0, 1, 1, "交易金额"),
                new ExcelHeaderInfo(0, 0, 2, 2, "费用项名称"),
                new ExcelHeaderInfo(0, 0, 3, 3, "分润模式"),
                new ExcelHeaderInfo(0, 0, 4, 4, "额度(%)"),
                new ExcelHeaderInfo(0, 0, 5, 5, "分润金额"),
                new ExcelHeaderInfo(0, 0, 6, 6, "姓名"),
                new ExcelHeaderInfo(0, 0, 7, 7, "用户类型"),
                new ExcelHeaderInfo(0, 0, 8,8, "用户级别"),
                new ExcelHeaderInfo(0, 0, 9,9, "公司名称")
        );
    }

    // 获取格式化信息
    private Map<String, ExcelFormat> getFormatInfo() {
        Map<String, ExcelFormat> format = new HashMap<>();
       /* format.put("id", ExcelFormat.FORMAT_INTEGER);
        format.put("categoryId", ExcelFormat.FORMAT_INTEGER);
    */
        return null;
    }


}
